package com.jz.order.controller;

import com.jz.commom.api.dto.request.Payment;
import com.jz.commom.api.dto.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class OrderController {

    // http://localhost:8080/payment/get/1
    public static final String PAYMENT_URL="http://localhost:8080";

    @Value( value = "${payUrl}" )
    private String payUrl;
    @Value( value = "${rpcPayUrl}" )
    private String rpcPayUrl;

    @Resource
    private RestTemplate restTemplate;


    @Resource
    DiscoveryClient discoveryClient;

    @PostMapping("/consumer/payment/create")
    public CommonResult create(@RequestBody Payment payment){
//        CommonResult commonResult = restTemplate.postForObject(payUrl+"/payment/create",payment,CommonResult.class);
        CommonResult commonResult = restTemplate.postForObject(rpcPayUrl+"/payment/create",payment,CommonResult.class);
        log.info("订单创建结果："+commonResult);
        return commonResult;
    }

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        try{
            CommonResult commonResult = restTemplate.getForObject(payUrl+"/payment/get/"+id,CommonResult.class);
            log.info("订单查询结果："+commonResult);
            return commonResult;
        }catch(Exception e){
            e.printStackTrace();
        }
        return CommonResult.fail("异常",null);

    }

    @GetMapping(value = "/consumer/payment/rpcget/{id}")
    public CommonResult<Payment> getRpcPaymentById(@PathVariable("id") Long id){
        try{
            CommonResult commonResult = restTemplate.getForObject(rpcPayUrl+"/payment/get/"+id,CommonResult.class);
            log.info("订单查询结果："+commonResult);
            return commonResult;
        }catch(Exception e){
            e.printStackTrace();
        }
        return CommonResult.fail("异常",null);
    }

    @GetMapping(value = "/consumer/payment/dubbo/rpcget/{id}")
    public CommonResult<Payment> getRpcPaymentById2(@PathVariable("id") Long id){
        try{
            CommonResult commonResult = restTemplate.getForObject(rpcPayUrl+"/payment/get/"+id,CommonResult.class);
            log.info("订单查询结果："+commonResult);
            return commonResult;
        }catch(Exception e){
            e.printStackTrace();
        }
        return CommonResult.fail("异常",null);
    }

    @GetMapping(value = "/consumer/payment/getService")
    public CommonResult getService(){
        List<String> regions = discoveryClient.getServices();
        List<ServiceInstance> list = discoveryClient.getInstances(regions.get(1));
        return CommonResult.success(null);

    }

}
